package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.sc.nft.dao.ExportRecordsDao;
import com.sc.nft.dao.TempRoughStoneOrderDao;
import com.sc.nft.entity.ExportRecords;
import com.sc.nft.entity.TempRoughStoneOrder;
import com.sc.nft.entity.vo.RoughStoneOrderVO;
import com.sc.nft.enums.*;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.mapper.TempRoughStoneOrderMapper;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.RoughStoneOrderService;
import com.sc.nft.service.TempRoughStoneOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 朱梦晨
 * @Description: 原石订单导出表
 * @date 2024-08-02
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TempRoughStoneOrderServiceImpl implements TempRoughStoneOrderService {


    private final TempRoughStoneOrderMapper tempRoughStoneOrderMapper;

    private final RoughStoneOrderService roughStoneOrderService;

    private final ExportRecordsDao exportRecordsDao;

    private final TempRoughStoneOrderDao tempRoughStoneOrderDao;

    @Override
    public Boolean saveTempRoughStoneOrder(String orderNo, OrderStatusEnum status, ClassifyType typeClassify, Date startOrderTime,
                                           Date endOrderTime, Date startPayTime, Date endPayTime, String userTel) {

        //原石订单导出
        List<RoughStoneOrderVO> roughStoneOrderVOList = roughStoneOrderService.roughStoneOrderExcel(orderNo, status, typeClassify, startOrderTime,
                endOrderTime, startPayTime, endPayTime, userTel);
        List<TempRoughStoneOrder> tempRoughStoneOrderList = BeanUtil.copyToList(roughStoneOrderVOList, TempRoughStoneOrder.class);
        if (CollectionUtils.isEmpty(tempRoughStoneOrderList)) {
            throw new GlobalRunTimeException("导出数据为空");
        }
        Long exportRecordsId = getExportRecordsId();
        try {
            tempRoughStoneOrderList.forEach(t -> t.setExportRecordsId(exportRecordsId));
            tempRoughStoneOrderMapper.saveBatch(tempRoughStoneOrderList);
        } catch (Exception e) {
            //导出失败
            log.error("原石订单失败:", e);
            return exportRecordsDao.updateStatusById(exportRecordsId, ExportTypeStatusEnum.EXPORT_STATUS_FAIL.getCode());
        }
        return exportRecordsDao.updateStatusById(exportRecordsId, ExportTypeStatusEnum.EXPORT_STATUS_SUCCESS.getCode());
    }


    private static Long getExportRecordsId() {

        StringBuilder exportName = new StringBuilder();
        int exportType = 0;

        exportName.append(ExportTypeEnum.TEMP_ROUGH_STONE_ORDER.getName()).append(DateUtil.format(DateUtil.date(), "yyyy年MM月dd日")).append(".xlsx");
        exportType = ExportTypeEnum.TEMP_ROUGH_STONE_ORDER.getCode();
        ExportRecords exportRecords = new ExportRecords();
        exportRecords.setExportName(exportName.toString());
        exportRecords.setExportType(exportType);
        exportRecords.setStatus(ExportTypeStatusEnum.EXPORT_STATUS_IN.getCode());
        exportRecords.setCreatorId(SysMemberPool.getMember().getId());
        exportRecords.setType(ExportRecordsTypeEnum.ADMIN);
        exportRecords.insert();
        Long exportRecordsId = exportRecords.getId();
        return exportRecordsId;
    }


    /**
     * 下载  根据导出记录Id
     *
     * @param exportRecordsId
     * @return
     */
    @Override
    public List<TempRoughStoneOrder> findTempRoughStoneOrderList(Long exportRecordsId) {
        return tempRoughStoneOrderDao.getTempOrderDetailList(exportRecordsId);
    }


}
